﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XCT.Class;
using XCT.Functions;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using System.Collections;

namespace XCT.Forms
{
    public partial class FormBTP : DevExpress.XtraEditors.XtraForm
    {
        public FormBTP()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormBTP(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }

        #region " Biến"

        clsBTP clsBTP = new clsBTP();
        clsDungChung clsdc = new clsDungChung();
        string TuanKH;
        DataSet dsBTP = new DataSet();
        int Them = 0;
       

        #endregion

        #region " Hàm"

		private void XuatTapTinExcel()
        {
            SaveFileDialog saveDlg;
            try
            {
                saveDlg = new SaveFileDialog();
                saveDlg.InitialDirectory = "C:\"";
                saveDlg.Filter = "Excel Files|*.xls";
                saveDlg.FilterIndex = 2;
                if (saveDlg.ShowDialog() == DialogResult.OK)
                {
                    DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                    grdBTP.ExportToXls(saveDlg.FileName, options);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                saveDlg = null;
            }
        }
		
        public void fKhoiTaoComBo()
        {
            for (int i = 2010; i <= DateTime.Now.Year + 1; i++)
                cmbNamKH.Properties.Items.Add(i.ToString());
            cmbNamKH.Text = DateTime.Now.Year.ToString();
            cmbThangKH.Text = DateTime.Now.Month.ToString();
            txtNgayNhap.Text = DateTime.Now.ToShortDateString();
        }

        public void fDocLSXTheoTuanKH(string TuanKH)
        {
            DataSet ds = new DataSet();
            lupLSX.Properties.DataSource = null;
            ds = clsBTP.DocLSXTheoTuanKH(TuanKH);
            lupLSX.Properties.DataSource = ds.Tables[0].DefaultView;
            lupLSX.Properties.ValueMember = "SoLSX";
            lupLSX.Properties.DisplayMember = "SoLSX";
            lupLSX.ItemIndex = 0;
        }

        private void KhoiTaoGrid(DevExpress.XtraGrid.Views.Grid.GridView grdView)
        {
            grdView.Columns["PNhap_BTP_LID"].Visible = false; //ID
            grdView.Columns["MaCD"].Visible = false; //macd
            grdView.Columns["TenCD"].Width = 60; //tencd
            grdView.Columns["TenCD"].Caption = "Teân CÑ";
            grdView.Columns["TenCD"].OptionsColumn.AllowEdit = false;
            grdView.Columns["MaChiTietMousse"].Visible = false; //machitiet
            grdView.Columns["TenChiTietMousse"].Width = 120; //tenchitiet
            grdView.Columns["TenChiTietMousse"].Caption = "Chi Tieát Mousse";
            grdView.Columns["TenChiTietMousse"].OptionsColumn.AllowEdit = false;
            grdView.Columns["MaMau_Mousse"].Width = 180; //diengiai - mau mousse
            grdView.Columns["MaMau_Mousse"].Caption = "Maøu Mousse/ Dieãn Giaûi";
            grdView.Columns["MaMau_Mousse"].OptionsColumn.AllowEdit = false;
            grdView.Columns["CLMousse"].Width = 60; //machungloai_mousse
            grdView.Columns["CLMousse"].Caption = "CLMss";
            grdView.Columns["CLMousse"].OptionsColumn.AllowEdit = false;
            grdView.Columns["DoLy"].Visible = false; //Do ly
            grdView.Columns["DoLy"].Width = 60; //Do ly
            grdView.Columns["DoLy"].Caption = "Ñoä Ly";
            grdView.Columns["DoLy"].OptionsColumn.AllowEdit = false;
            grdView.Columns["MaHoaVanMousse"].Visible = false; //ma hoa van
            grdView.Columns["TenHoaVanMousse"].Width = 60; //ten hoa van
            grdView.Columns["TenHoaVanMousse"].Caption = "H.Vaên";
            grdView.Columns["TenHoaVanMousse"].OptionsColumn.AllowEdit = false;
            grdView.Columns["DVTID"].Visible = false; //ma dvt
            grdView.Columns["DVT"].Width = 50; //ten dvt
            grdView.Columns["DVT"].Caption = "ÑVT";
            grdView.Columns["DVT"].OptionsColumn.AllowEdit = false;
            grdView.Columns["QuiCachCLLH"].Visible = false; // qui cach CLLH
            grdView.Columns["QuiCachCLLH"].Width = 80; // qui cach CLLH
            grdView.Columns["QuiCachCLLH"].Caption = "QC Taám";
            grdView.Columns["QuiCachCLLH"].OptionsColumn.AllowEdit = false;
            grdView.Columns["SoLop"].Visible = false; // qui cach CLLH
            grdView.Columns["SoLop"].Width = 50; // qui cach CLLH
            grdView.Columns["SoLop"].Caption = "S.Lôùp";
            grdView.Columns["SoLop"].OptionsColumn.AllowEdit = false;
            grdView.Columns["QuiCach"].Width = 80; //qui cach
            grdView.Columns["QuiCach"].Caption = "QC Mieáng";
            grdView.Columns["QuiCach"].OptionsColumn.AllowEdit = false;
            grdView.Columns["QuiCach"].AppearanceCell.BackColor = Color.Tomato;
           // grdView.Columns["SLKH"].Visible = false; //So luong KH
            grdView.Columns["SLKH"].Width = 60; //So luong KH
            grdView.Columns["SLKH"].Caption = "SLKH";
            grdView.Columns["SLKH"].OptionsColumn.AllowEdit = false;
            grdView.Columns["SL_CanTH"].Width = 60; //So luong can thuc hien
            grdView.Columns["SL_CanTH"].Caption = "Caàn TH";
            grdView.Columns["SL_CanTH"].OptionsColumn.AllowEdit = false;
            grdView.Columns["SL_CanTH"].AppearanceCell.BackColor = Color.Orange;
            grdView.Columns["Ca1"].Width = 60; //ca 1    
            grdView.Columns["Ca1"].Caption = "Ca A";
            grdView.Columns["Ca1"].AppearanceCell.BackColor = Color.SkyBlue;
            grdView.Columns["Ca2"].Width = 60; //ca 2
            grdView.Columns["Ca2"].Caption = "Ca B";
            grdView.Columns["Ca2"].AppearanceCell.BackColor = Color.Pink;
            grdView.Columns["Ca3"].Visible = false;
            grdView.Columns["Ca3"].Width = 60; //ca 3
            grdView.Columns["Ca3"].Caption = "Ca C";
            grdView.Columns["Ca3"].AppearanceCell.BackColor = Color.Silver;
            grdView.Columns["SL_Nhap"].Width = 60; //So luong da nhap
            grdView.Columns["SL_Nhap"].Caption = "Toång";
            grdView.Columns["SL_Nhap"].OptionsColumn.AllowEdit = false;
            grdView.Columns["GhiChu"].Width = 100; //ghi chu
            grdView.Columns["GhiChu"].Caption = "Ghi Chuù";
            grdView.Columns["SL_LuyKe"].Width = 60; //So luong da nhap
            grdView.Columns["SL_LuyKe"].Caption = "Luõy Keá";
            grdView.Columns["SL_LuyKe"].OptionsColumn.AllowEdit = false;
           


            grdView.Columns["SLKH"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SLKH"].DisplayFormat.FormatString = "n0";
            grdView.Columns["SL_CanTH"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_CanTH"].DisplayFormat.FormatString = "n0";
            grdView.Columns["Ca1"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["Ca1"].DisplayFormat.FormatString = "n0";
            grdView.Columns["Ca2"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["Ca2"].DisplayFormat.FormatString = "n0";
            grdView.Columns["Ca3"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["Ca3"].DisplayFormat.FormatString = "n0";
            grdView.Columns["SL_Nhap"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_Nhap"].DisplayFormat.FormatString = "n0";
            grdView.Columns["SL_LuyKe"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            grdView.Columns["SL_LuyKe"].DisplayFormat.FormatString = "n0";
         
        }

        // Lưu BTP
        private Boolean LuuBTP()
        {
            grdBTP_View.CloseEditor();
            if (dsBTP.Tables[0].Rows.Count > 0)
            {
                for (int intRow = 0; intRow < dsBTP.Tables[0].Rows.Count; intRow++)
                {
                    clsBTP = new clsBTP();
                    clsBTP.PNhap_BTP_LID = int.Parse(dsBTP.Tables[0].Rows[intRow]["PNhap_BTP_LID"].ToString());
                   // clsBTP.MaChietTinhML = int.Parse(dsBTP.Tables[0].Rows[intRow]["MaChietTinhML"].ToString());
                    clsBTP.MaChietTinhML = 0;
                    clsBTP.SoLSX = lupLSX.EditValue.ToString();
                    clsBTP.NgayTH = Convert.ToDateTime(txtNgayNhap.DateTime.ToShortDateString());
                    clsBTP.MaSP = txtMaSP.Text;
                    clsBTP.MaChungLoai_Mousse = dsBTP.Tables[0].Rows[intRow]["CLMousse"].ToString();
                    clsBTP.MaHoaVan_Mousse = int.Parse(dsBTP.Tables[0].Rows[intRow]["MaHoaVanMousse"].ToString());
                    clsBTP.MaChiTiet_Mousse = int.Parse(dsBTP.Tables[0].Rows[intRow]["MaChiTietMousse"].ToString());
                    clsBTP.DvtID = int.Parse(dsBTP.Tables[0].Rows[intRow]["DVTID"].ToString());
                    clsBTP.MaCD = dsBTP.Tables[0].Rows[intRow]["MaCD"].ToString();
                //    clsBTP.DienGiai = dsBTP.Tables[0].Rows[intRow]["DienGiai"].ToString();
                    clsBTP.MaMau_Mousse = dsBTP.Tables[0].Rows[intRow]["MaMau_Mousse"].ToString();
                    if (dsBTP.Tables[0].Rows[intRow]["DoLy"].ToString() != "")
                        clsBTP.DoLy = float.Parse(dsBTP.Tables[0].Rows[intRow]["DoLy"].ToString());
                    if (dsBTP.Tables[0].Rows[intRow]["SoLop"].ToString() != "")
                        clsBTP.SoLop = int.Parse(dsBTP.Tables[0].Rows[intRow]["SoLop"].ToString());
                    clsBTP.QuiCach = dsBTP.Tables[0].Rows[intRow]["QuiCach"].ToString();
                    clsBTP.SLKH = float.Parse(dsBTP.Tables[0].Rows[intRow]["SLKH"].ToString());
                    clsBTP.SL_CanTH = float.Parse(dsBTP.Tables[0].Rows[intRow]["SL_CanTH"].ToString());
                    if (dsBTP.Tables[0].Rows[intRow]["Ca1"].ToString() != "")
                        clsBTP.SL_Nhap_Ca1 = float.Parse(dsBTP.Tables[0].Rows[intRow]["Ca1"].ToString());
                    if (dsBTP.Tables[0].Rows[intRow]["Ca2"].ToString() != "")
                        clsBTP.SL_Nhap_Ca2 = float.Parse(dsBTP.Tables[0].Rows[intRow]["Ca2"].ToString());
                    if (dsBTP.Tables[0].Rows[intRow]["Ca3"].ToString() != "")
                        clsBTP.SL_Nhap_Ca3 = float.Parse(dsBTP.Tables[0].Rows[intRow]["Ca3"].ToString());
                    clsBTP.Tong_SL_Nhap = clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3;
                    clsBTP.SL_ConLai = clsBTP.SL_CanTH - clsBTP.Tong_SL_Nhap;
                    if (dsBTP.Tables[0].Rows[intRow]["SL_luyKe"].ToString() != "")
                        clsBTP.SL_LuyKe = float.Parse(dsBTP.Tables[0].Rows[intRow]["SL_LuyKe"].ToString()) + clsBTP.Tong_SL_Nhap;
                    else
                        clsBTP.SL_LuyKe = clsBTP.Tong_SL_Nhap;

                    clsBTP.GhiChu = dsBTP.Tables[0].Rows[intRow]["GhiChu"].ToString();
                   
                    clsBTP.TuanKH = TuanKH;
                    clsBTP.UserUpdate = clsDungChung.strUser;                       
                    clsBTP.DateUpdate = DateTime.Now;
                    if (clsBTP.Tong_SL_Nhap > 0)
                    {
                        if (clsBTP.LuuBTP() == false)
                            return false;
                    }
                }
                return true;
            }
            else
             return false;
        }

        private void KiemTraNgay()
        {
            Them = 0;
            DataTable dt = new DataTable();

            DateTime Ngay_TH;

            dt = clsBTP.GetSLKH("select min(NgayNhap) as NgayNhap from PNhap_BTP_L where solsx = '" + lupLSX.Text + "'");

            if (dt.Rows.Count > 0 && dt.Rows[0]["NgayNhap"].ToString() != "") // da có nhap roi
            {

                Ngay_TH = Convert.ToDateTime(dt.Rows[0]["NgayNhap"].ToString());
                lbNgayNhap.Text = " Ngày đầu tiên nhập LSX " + lupLSX.Text + " là " + Ngay_TH.ToShortDateString();
                lbNgayNhap.Visible = true;

                if (Convert.ToDateTime(txtNgayNhap.DateTime) < Ngay_TH)
                {
                    XtraMessageBox.Show("Không được lùi ngày nhập", "Thông Báo:");

                    txtNgayNhap.Text = DateTime.Now.ToShortDateString();
                }
                else
                {
                    dsBTP = clsBTP.GetBTPTheoLSX(lupLSX.Text, txtNgayNhap.Text,0);
                    grdBTP.DataSource = dsBTP.Tables[0].DefaultView;

                    grdBTP.Focus();
                    KhoiTaoGrid(grdBTP_View);
                    KiemTraDongBo();
                }

            }
            else
            {
                lbNgayNhap.Visible = false;

                dsBTP = clsBTP.GetBTPTheoLSX(lupLSX.Text, txtNgayNhap.Text,0);
                grdBTP.DataSource = dsBTP.Tables[0].DefaultView;

                grdBTP.Focus();
                KhoiTaoGrid(grdBTP_View);
                KiemTraDongBo();
            }
        }

        private void KiemTraLuu()
        {

            grdBTP.Focus();
            if (LuuBTP() == true)
            {
                XtraMessageBox.Show("Lưu Thành công", "Thông Báo:");
                Them = 0;
            }

            dsBTP = clsBTP.GetBTPTheoLSX(lupLSX.Text, txtNgayNhap.Text,0);
            grdBTP.DataSource = dsBTP.Tables[0].DefaultView;

            grdBTP.Focus();
            KhoiTaoGrid(grdBTP_View);
            KhoaLuoi(grdBTP_View);
            KiemTraDongBo();
            

        }

        private void KiemTraThem()
        {

            Them = 0;
            DataTable dt = new DataTable();

            DateTime Ngay_TH;

            dt = clsBTP.GetSLKH("select max(NgayNhap) as NgayNhap from PNhap_BTP_L where solsx = '" + lupLSX.Text + "'");

            if (dt.Rows.Count > 0 && dt.Rows[0]["NgayNhap"].ToString() != "") // da có nhap roi
            {

                Ngay_TH = Convert.ToDateTime(dt.Rows[0]["NgayNhap"].ToString());


                if (Convert.ToDateTime(txtNgayNhap.DateTime) < Ngay_TH)
                {
                    XtraMessageBox.Show("Không được lùi ngày nhập", "Thông Báo:");

                    txtNgayNhap.Text = DateTime.Now.ToShortDateString();
                }
                else
                {
                    Them = 1;
                    grdBTP.DataSource = null;
                    dsBTP = clsBTP.GetBTPTheoLSX(lupLSX.Text, txtNgayNhap.Text, Them);
                    grdBTP.DataSource = dsBTP.Tables[0].DefaultView;

                    grdBTP.Focus();
                    grdBTP_View.FocusedRowHandle = grdBTP_View.FocusedRowHandle + 1;
                    grdBTP_View.FocusedColumn = grdBTP_View.VisibleColumns[8];
                    grdBTP_View.ShowEditor();
                    KhoiTaoGrid(grdBTP_View);
                    KhoaLuoi(grdBTP_View);
                }
            }

        }
        private void KiemTraXem()
        {
            Them = 0;
            grdBTP.DataSource = null;
            dsBTP = clsBTP.GetBTPTheoLSX(lupLSX.Text, txtNgayNhap.Text, Them);
            grdBTP.DataSource = dsBTP.Tables[0].DefaultView;

            grdBTP.Focus();
            KhoiTaoGrid(grdBTP_View);
            KhoaLuoi(grdBTP_View);

        }

        private void KhoaLuoi(DevExpress.XtraGrid.Views.Grid.GridView grdView)
        {
            if (Them == 0)
            {
                DataTable dt = new DataTable();

                dt = clsBTP.GetSLKH("SET DATEFORMAT dmy    select Top 1 * from PNhap_BTP_L where solsx = '" + lupLSX.Text + "' and NgayNhap = '" + txtNgayNhap.DateTime.ToShortDateString() + "'");

                if (dt.Rows.Count > 0) // co nhap roi
                {
                    grdView.Columns["Ca1"].OptionsColumn.AllowEdit = false;
                    grdView.Columns["Ca2"].OptionsColumn.AllowEdit = false;
                    grdView.Columns["Ca3"].OptionsColumn.AllowEdit = false;
                    grdView.Columns["GhiChu"].OptionsColumn.AllowEdit = false;
                    btnLuu.Enabled = false;
                }
                else
                {
                    grdView.Columns["Ca1"].OptionsColumn.AllowEdit = true;
                    grdView.Columns["Ca2"].OptionsColumn.AllowEdit = true;
                    grdView.Columns["Ca3"].OptionsColumn.AllowEdit = true;
                    grdView.Columns["GhiChu"].OptionsColumn.AllowEdit = true;
                    btnLuu.Enabled = true;

                }
            }
            else
            {
                grdView.Columns["Ca1"].OptionsColumn.AllowEdit = true;
                grdView.Columns["Ca2"].OptionsColumn.AllowEdit = true;
                grdView.Columns["Ca3"].OptionsColumn.AllowEdit = true;
                grdView.Columns["GhiChu"].OptionsColumn.AllowEdit = true;
                btnLuu.Enabled = true;
            }


        }


        private void KiemTraDongBo()
        {
            if (lupLSX.Text != "" && lupLSX.Text != "[Chọn Số LSX]")
            {
                DataSet ds = new DataSet();

                ds = clsBTP.KiemTraDongBo(lupLSX.Text);
           
                if (ds.Tables[0].Rows.Count > 0 )
                {
                    if (ds.Tables[0].Rows[0]["DongBo"].ToString() == "1")
                    {
                        lbDongBo.Text = " LSX " + lupLSX.Text + " đồng bộ ngày " + Convert.ToDateTime(ds.Tables[0].Rows[0]["NgayNhap"].ToString()).ToShortDateString();
                       // lbDongBo.Visible = true;
                    }
                    else
                    {
                       // lbDongBo.Visible = false;
                        lbDongBo.Text = " Để nhập thêm Nhấp Ctrl A, để xem lại đã nhập nhấn Ctrl R";
                    }

                  
                }
                else
                    //lbDongBo.Visible = false;
                    lbDongBo.Text = " Để nhập thêm Nhấp Ctrl A, để xem lại đã nhập nhấn Ctrl R";
            }

        }



        #endregion /// " Hàm"

        #region " Sự Kiện"

        private void FormBTP_Load(object sender, EventArgs e)
        {
            fKhoiTaoComBo();
            TuanKH = cmdTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
            fDocLSXTheoTuanKH(TuanKH);
        }

        private void FormBTP_FormClosed(object sender, FormClosedEventArgs e)
        {
            formMain.pFormBTP = null;
        }

        private void FormBTP_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
			if (e.Modifiers == Keys.Control)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.P:
                            if (XtraMessageBox.Show("Bạn có chắc muốn xuất ra tập tin Excel ?", "Thông tin lưu ý: ", MessageBoxButtons.YesNo) == DialogResult.Yes) 
                                XuatTapTinExcel();
                            return;
                        case Keys.S:
                            {
                                if (XtraMessageBox.Show("Bạn có chắc muốn lưu không ?", "Thông tin lưu ý: ", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                {
                                    KiemTraLuu();
                                }
                            }
                            return;

                        case Keys.A:
                            {
                                if (XtraMessageBox.Show("Bạn có chắc muốn thêm cho LSX này không ?", "Thông tin lưu ý: ", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                {
                                    KiemTraThem();
                                }
                            }
                            return;
                        case Keys.R:
                            {
                               
                                    KiemTraXem();
                                
                            }
                            return;

                           // return;
					}
				}
                else if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        //private void cmbNamKH_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    grdBTP.DataSource = null;
        //   // txtSoLSX.Text = "";
        //    TuanKH = cmdTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
        //    if (txtSoLSX.Text == "")
        //    fDocLSXTheoTuanKH(TuanKH);
        //}

        //private void cmbThangKH_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    grdBTP.DataSource = null;
        // //   txtSoLSX.Text = "";
        //    TuanKH = cmdTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
        //    if (txtSoLSX.Text == "")
        //    fDocLSXTheoTuanKH(TuanKH);

        //}

        private void cmdTuanKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdBTP.DataSource = null;
            //  txtSoLSX.Text = "";
            TuanKH = cmdTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
            //if (txtSoLSX.Text == "")
                fDocLSXTheoTuanKH(TuanKH);
        }

        private void lupLSX_EditValueChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();    
            

            txtMaSP.Text = lupLSX.GetColumnValue("MaSP").ToString();
            dt = clsBTP.GetSLKH("select sum(soluong) as soluong ,ttn ,a.solsx, masp from dp_lsx_h a inner join dp_lsx_l b on a.solsx = b.solsx where a.solsx = '" + lupLSX.Text + "' " + " group by ttn, a.solsx ,masp ");

            txtSLKH.Text = dt.Rows[0]["soluong"].ToString();
            TuanKH = dt.Rows[0]["ttn"].ToString();
            cmdTuanKH.EditValue = TuanKH.Substring(0, 2);

            TuanKH = dt.Rows[0]["ttn"].ToString();
            cmbThangKH.EditValue = TuanKH.Substring(2, 2);

            TuanKH = dt.Rows[0]["ttn"].ToString();
            cmbNamKH.EditValue = TuanKH.Substring(4, 4);

            lupLSX.EditValue = dt.Rows[0]["solsx"].ToString();
           txtSoLSX.Text = dt.Rows[0]["solsx"].ToString();
            KiemTraNgay();
            KhoaLuoi(grdBTP_View);
          
        }

        private void grdBTP_View_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            float ca1, ca2, ca3;
            string cot;
            if (dsBTP.Tables[0].Rows.Count > 0)
            {
                for (int intRow = 0; intRow < dsBTP.Tables[0].Rows.Count; intRow++)
                {
                    clsBTP = new clsBTP();
                    clsBTP.SL_CanTH = float.Parse(dsBTP.Tables[0].Rows[intRow]["SL_CanTH"].ToString());

                    if (dsBTP.Tables[0].Rows[intRow]["Ca1"].ToString() != "")
                        clsBTP.SL_Nhap_Ca1 = float.Parse(dsBTP.Tables[0].Rows[intRow]["Ca1"].ToString());
                    ca1 = clsBTP.SL_Nhap_Ca1;
                    if (dsBTP.Tables[0].Rows[intRow]["Ca2"].ToString() != "")
                        clsBTP.SL_Nhap_Ca2 = float.Parse(dsBTP.Tables[0].Rows[intRow]["Ca2"].ToString());
                    ca2 = clsBTP.SL_Nhap_Ca2;
                    if (dsBTP.Tables[0].Rows[intRow]["Ca3"].ToString() != "")
                        clsBTP.SL_Nhap_Ca3 = float.Parse(dsBTP.Tables[0].Rows[intRow]["Ca3"].ToString());
                    ca3 = clsBTP.SL_Nhap_Ca3;

                    cot = e.Column.FieldName;

                    if (cot == "Ca1") // cot ca1
                    {
                        if (clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3 > clsBTP.SL_CanTH)
                        {
                            XtraMessageBox.Show("Tổng số lượng nhập không được lớn hơn số lượng cần thực hiện", "Thông Báo:");
                            dsBTP.Tables[0].Rows[intRow]["Ca1"] = 0;// clsBTP.SL_CanTH - (clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3);
                        }
                        else
                            dsBTP.Tables[0].Rows[intRow]["SL_Nhap"] = clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3;
                    }

                    if (cot == "Ca2") // cot ca2
                    {
                        if (clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3 > clsBTP.SL_CanTH)
                        {
                           XtraMessageBox.Show("Tổng số lượng nhập không được lớn hơn số lượng cần thực hiện", "Thông Báo:");
                           dsBTP.Tables[0].Rows[intRow]["Ca2"] = 0;// clsBTP.SL_CanTH - (clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca3);
                       }
                       else
                           dsBTP.Tables[0].Rows[intRow]["SL_Nhap"] = clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3;
                    }

                    if (cot == "Ca3") // cot ca3
                    {
                        if (clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3 > clsBTP.SL_CanTH)
                        {
                           XtraMessageBox.Show("Tổng số lượng nhập không được lớn hơn số lượng cần thực hiện", "Thông Báo:");
                           dsBTP.Tables[0].Rows[intRow]["Ca3"] = 0;// clsBTP.SL_CanTH - (clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2);
                       }
                       else
                           dsBTP.Tables[0].Rows[intRow]["SL_Nhap"] = clsBTP.SL_Nhap_Ca1 + clsBTP.SL_Nhap_Ca2 + clsBTP.SL_Nhap_Ca3;
                    }
                  
                }
            }
        }
  
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (lupLSX.Text != "")              
                KiemTraLuu();
            else
             XtraMessageBox.Show("Chưa chọn LSX", "Thông Báo:");

        }

        private void txtNgayNhap_EditValueChanged(object sender, EventArgs e)
        {
            KiemTraNgay();
            KhoaLuoi(grdBTP_View);
        }

        //Lưu bằng các phím nóng
        private void grdBTP_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Modifiers == Keys.Control)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.S:
                            {
                                if (XtraMessageBox.Show("Bạn có chắc muốn lưu không ?", "Thông tin lưu ý: ", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                {
                                    KiemTraLuu();
                                }
                            }
                            return;
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void grdBTP_View_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
                
                
            }


        }


        private void txtSoLSX_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                DataSet ds = new DataSet();
                lupLSX.Properties.DataSource = null;
                ds = clsBTP.DocLSXTheoLSX(txtSoLSX.Text);
                lupLSX.Properties.DataSource = ds.Tables[0].DefaultView;
                lupLSX.Properties.ValueMember = "SoLSX";
                lupLSX.Properties.DisplayMember = "SoLSX";
                lupLSX.ItemIndex = 0;
                
            }

        }



        private void grdBTP_View_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {


            clsBTP clsBTP_1 = new clsBTP();
            if (dsBTP.Tables[0].Rows.Count > 0 )
            {
                for (int intRow = 0; intRow < dsBTP.Tables[0].Rows.Count; intRow++)
                {

                    clsBTP_1.SLKH = float.Parse(dsBTP.Tables[0].Rows[intRow]["SLKH"].ToString());
                    if (dsBTP.Tables[0].Rows[intRow]["SL_LuyKe"].ToString() != "")
                        clsBTP_1.SL_LuyKe = float.Parse(dsBTP.Tables[0].Rows[intRow]["SL_LuyKe"].ToString());
                    if (clsBTP_1.SL_LuyKe == clsBTP_1.SLKH)
                    {
                        if (e.RowHandle == intRow)
                        {
                            e.Appearance.BackColor = Color.Silver;
                            e.Appearance.ForeColor = Color.BlueViolet;
                        }
                    }

                }
            }
        }

      
        #endregion  // su kien

     



















    }
}